﻿using API.Logic;
using RestSharp;

namespace API
{
    public class TimedExecutService : BackgroundService
    {
        private readonly CameraFunction _CameraFunction;
        public TimedExecutService(CameraFunction cameraFunction)
        {

            this._CameraFunction = cameraFunction;
        }


        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {

                Console.WriteLine($"{DateTime.Now} BackgroundService：启动");

                while (!stoppingToken.IsCancellationRequested)
                {
                    await Task.Delay(1000 * 60, stoppingToken); //启动后10秒执行一次

                    var date = DateTime.Now;

                    int Hour = date.Hour;

                    int Minute = date.Minute;

                    int second = date.Second;

                    this._CameraFunction.Token();
                }
                Console.WriteLine($"{DateTime.Now} BackgroundService：停止");
            }
            catch (Exception ex)
            {
                if (!stoppingToken.IsCancellationRequested)
                {
                    Console.WriteLine($"{DateTime.Now} BackgroundService：异常=>{ex.Message}=>{ex.StackTrace}");
                }
                else
                {
                    Console.WriteLine($"{DateTime.Now} BackgroundService：ex停止");
                }




            }
        }
    }
}
